package com.zxj.schoolxianyu.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zxj.schoolxianyu.dao.commentsDao;
import com.zxj.schoolxianyu.entity.comments;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class getcommentsService {

    @Autowired(required = false)
    private commentsDao commentsDao;

    public String getcomments(int goodsid) {
        EntityWrapper<comments> wrapper = new EntityWrapper<>();
        wrapper.eq("goodsid", goodsid);
        List<comments> c = commentsDao.selectList(wrapper);
        if (c.isEmpty()) return "No more data";
        else {
            List<comments> cs = new ArrayList<>();
            for(int i = 0;i<c.size();i++){
                if(c.get(i).parentid==null){
                    cs.add(c.get(i));
                    for(int j = 0;j<c.size();j++){
                        if (c.get(j).parentid==null)
                            continue;
                        if (c.get(j).parentid == c.get(i).commentid)
                            cs.add(c.get(j));
                    }
                }
            }
            return JSON.toJSONString(cs);
        }
    }
}
